package com.pachong.spider;

import com.common.core.WyyMusicConstant;
import com.common.core.utils.WyyMusicUtils;
import com.domain.bean.music.MusicUser;
import com.domain.bean.music.PlaylistPool;
import com.domain.bean.music.Song;
import com.domain.bean.music.SongMenu;
import com.pachong.service.MusicService;
import com.pachong.service.MusicUserService;
import com.pachong.service.SongMenuService;
import com.pachong.service.SongService;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.downloader.HttpClientDownloader;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;
import us.codecraft.xsoup.Xsoup;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author : wangyu
 * Date: 2018-03-06
 * Time: 13:54
 */
@Component
public class WyyPlaylistPageProcessor implements PageProcessor {
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SongMenuService songMenuService;
    @Resource
    private SongService songService;
    @Resource
    private MusicUserService musicUserService;
    @Resource
    private MusicService musicService;

    @Override
    public void process(Page page) {
        //当爬取的是歌单的时候
        if(page.getUrl().regex(WyyMusicConstant.PLAYLIST_URL).match()){
            String s = page.getUrl().toString();
            String[] split = s.split("id=");
            this.handlePlayList(page, split[1]);
        }
    }
    /**
     * 页面处理
     * @param page
     */
    private void handlePlayList(Page page,String code){
        SongMenu songMenu = songMenuService.findByCode(code);
        if(songMenu == null){
            return;
        }
        Html html = page.getHtml();
        //选择标签DIV class类型为tit中的a中的text文本信息
        String title = html.xpath("//div[@class=\"tit\"]/h2/text()").toString();
        songMenu.setName(title);//歌曲菜单名称
        //获取的是用户html
        Selectable xpath = html.xpath("//div[@class=\"user f-cb\"]");
        String userName = xpath.xpath("//span[@class=\"name\"]/a/text()").toString();
        songMenu.setUserName(userName);//创建人
        String url = xpath.xpath("//span[@class=\"name\"]/a").links().toString();//用户的超链接
        songMenu.setUserCode(url.split("id=")[1]);
        String createTime = xpath.xpath("//span[@class=\"time s-fc4\"]/text()").toString();//创建时间
        songMenu.setCreationDate(createTime.replace(" 创建","").trim());//创建时间

//        //TODO 收藏数,分享数,评价数
        Selectable operation = html.xpath("//div[@id=\"content-operation\"]");
        //收藏数
        String save = operation.xpath("//a[@class=\"u-btni u-btni-fav \"]/i/text()").toString();
        save = save.trim().replace("(","").replace(")","");

        String errorInfo = "";
        if(WyyMusicUtils.regexNumber(save)){
            songMenu.setCollectNum(Integer.valueOf(save));
        }else {
            errorInfo += " 收藏数:"+save;
        }

        //分享
        String share = operation.xpath("//a[@class=\"u-btni u-btni-share \"]/i/text()").toString();
        share = share.trim().replace("(","").replace(")","");
        if(WyyMusicUtils.regexNumber(share)){
            songMenu.setShareNum(Integer.valueOf(share));
        }else {
            errorInfo += " 分享:"+share;
        }

        //评论数
        String cmtTotal = operation.xpath("//a[@class=\"u-btni u-btni-cmmt \"]/i/span/text()").toString();
        cmtTotal = cmtTotal.trim().replace("(","").replace(")","");
        if(WyyMusicUtils.regexNumber(cmtTotal)){
            songMenu.setCmtTotal(Integer.valueOf(cmtTotal));
        }else {
            errorInfo += " 分享:"+share;
        }

        if(StringUtils.isNotBlank(errorInfo)){
            songMenu.setRemark(errorInfo);
        }

       //TODO  标签
        Document document = Jsoup.parse(html.xpath("//div[@class=\"tags f-cb\"]").toString());
        List<String> lableList = Xsoup.compile("//a/i/text()").evaluate(document).list();
        if(lableList != null && !lableList.isEmpty()){
            String labels = "";
            for (String labelText : lableList) {
                labels += labelText +",";
            }
            songMenu.setLabels(labels);
        }
          //TODO 简介(HTML格式)
        String s = html.xpath("//p[@id=\"album-desc-more\"]").toString();
        songMenu.setIntroduce(s);
         //TODO 播放数量
        String playCount= html.xpath("//strong[@id=\"play-count\"]/text()").toString();
        songMenu.setPlayNum(Long.valueOf(playCount));

        //TODO 歌曲
        List<String> all = html.xpath("//div[@id=\"song-list-pre-cache\"]").links().regex(WyyMusicConstant.SONG_URL).all();
        List<Song> songs = new ArrayList<>();
        for (String songUrl: all) {
            System.err.println("爬取到的跟去URL:"+songUrl);
            Song song = new Song();
            song.setUrl(songUrl);
            String songCode = songUrl.split("id=")[1].trim();
            if(StringUtils.isBlank(songCode)){
                continue;
            }
            song.setCode(songCode);
            Song songBycode = songService.findByCode(songCode);
            if(songBycode == null){
                try{
                    songService.save(song);
                }catch (Exception e){
                    LOGGER.error("保存歌曲信息时报错:",e);
                    System.err.println("保存歌曲报错:"+songCode);
                }
            }
        }

        if(StringUtils.isNotBlank(songMenu.getUserCode())){
            //保存用户信息
            MusicUser byCode = musicUserService.findByCode(songMenu.getUserCode());
            if(byCode == null){
                MusicUser user = new MusicUser();
                user.setCode(songMenu.getUserCode());
                user.setNickname(songMenu.getUserName());
                musicUserService.save(user);
            }
        }

//        Integer cmtTotalInt = musicService.handleAllComment(songMenu.getCode(), 0, WyyMusicConstant.SONG_MENU);
//        songMenu.setCmtTotal(cmtTotalInt);
        songMenu.setTimes(songMenu.getTimes()+1);//爬虫次数增加1
        //数据解析完成后在保存歌单信息
        songMenuService.saveAndFlush(songMenu);
    }

    @Override
    public Site getSite() {
        return WyyMusicUtils.createSite();
    }

    //启动spider爬虫,爬虫歌单详情页面
    public void start() {
        List<SongMenu> songMenus = songMenuService.selectLimt(0);
        long start = System.currentTimeMillis();
        Spider.create(this)
                .startUrls(songMenus.stream().map(playlistPool -> {
                    return playlistPool.getUrl();
                }).collect(Collectors.toList()))
                .setDownloader(WyyMusicUtils.getDownloader())
                .thread(WyyMusicConstant.THREAD_NUMS)
                .run();
        long end = System.currentTimeMillis();
        System.out.println("爬虫结束,耗时--->" + WyyMusicUtils.parseMillisecone(end - start));
    }
}
